package cn.fl.mcr.controller;

import cmm.mid.core.framework.page.ListPageVO;
import cmm.web.core.framework.controller.BaseController;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.mcr.transform.McrSourcePopCountVOTransform;
import cn.fl.mcr.vo.McrSourcePopCountVO;
import cn.lionbridgecapital.loan.model.base.Pager;
import cn.lionbridgecapital.loan.model.condition.McrSourcePopRedQC;
import cn.lionbridgecapital.loan.model.dto.McrSourcePopCountDTO;
import cn.lionbridgecapital.loan.model.enu.IsDeleteType;
import cn.lionbridgecapital.loan.model.enu.IsSuccessType;
import cn.lionbridgecapital.loan.service.api.IMcrSourcePopApiService;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author yangqh521
 * @date 2020/4/24
 * @desp 渠道推广
 */
@RestController
@RequestMapping("/mcr/mcrSourcePop")
public class McrSourcePopController extends BaseController {

    private static final Logger logger = LoggerFactory.getLogger(McrCstCrLimSchController.class);

    @Autowired
    private IMcrSourcePopApiService mcrSourcePopApiService;

    /**
     * 日期参数绑定
     * @param binder
     */
    @InitBinder
    public void initBinder(ServletRequestDataBinder binder){
        CustomDateEditor customDateEditor = new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), true);
        binder.registerCustomEditor(Date.class, customDateEditor);
    }

    /**
     * 到列表页面
     */
    @RequestMapping("/toListPage.do")
    public ModelAndView toListPage() {
        ModelAndView view = new ModelAndView("/mcr/mcrSourcePop/mcrSourcePopList");
        BaseLogUtils.info(logger, "toListPage", "跳转到列表页面.", CurrentThreadContext.getCurrentUserName());
        return view;
    }

    /**
     * 列表数据
     * @param page
     * @param pagesize
     * @return
     */
    @RequestMapping("/initList.do")
    public @ResponseBody
    ListPageVO initList(@RequestParam(defaultValue = "1") Integer page,
                        @RequestParam(defaultValue = "10")Integer pagesize,
                        McrSourcePopRedQC qc) {
        // 查询
        qc.setIsFirst(IsSuccessType.SUCCESS.getCode());
        qc.setIsDel(IsDeleteType.UN_DELETE.getCode());
        Pager queryPager = new Pager();
        queryPager.setCurrentPage(page);
        queryPager.setEveryPage(pagesize);
        queryPager.setCondition(qc);
        BaseLogUtils.info(logger, "initList", "获取列表, queryPager:" + JSONObject.toJSONString(queryPager),
                CurrentThreadContext.getCurrentUserName());

        // 初始化返回数据
        Integer totalCount = 0;
        List<McrSourcePopCountVO> resultList = new ArrayList();
        try {
            Pager resultPager = mcrSourcePopApiService.getCountListForPage(queryPager);
            BaseLogUtils.info(logger, "initList", "获取列表, resultPager:" + JSONObject.toJSONString(resultPager),
                    CurrentThreadContext.getCurrentUserName());
            // 处理返回数据
            if(resultPager != null && CollectionUtils.isNotEmpty(resultPager.getResultList())){
                totalCount = resultPager.getTotalCount();
                resultList = McrSourcePopCountVOTransform.toVOList((List<McrSourcePopCountDTO>)resultPager.getResultList());
            }
        } catch (Exception e) {
            BaseLogUtils.error(logger, "initList", "获取列表异常, queryPager:" + JSONObject.toJSONString(queryPager),
                    CurrentThreadContext.getCurrentUserName(), e);
        }

        // 返回数据
        ListPageVO<McrSourcePopCountVO> listPageVO = new ListPageVO<>(resultList, totalCount);
        BaseLogUtils.info(logger, "initList", "获取列表, listPageVO:" + JSONObject.toJSONString(listPageVO),
                CurrentThreadContext.getCurrentUserName());
        return listPageVO;
    }


}
